package com.wsfk.ssm.action;

import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import com.wsfk.ssm.entity.Bill;
import com.wsfk.ssm.entity.BookInfo;
import com.wsfk.ssm.entity.HandBook;
import com.wsfk.ssm.entity.Product;
import com.wsfk.ssm.entity.User;
import com.wsfk.ssm.messages.HandBookMessage;
import com.wsfk.ssm.messages.LoginMessage;
import com.wsfk.ssm.messages.Message;
import com.wsfk.ssm.messages.RegistMessage;
import com.wsfk.ssm.messages.UpdateMessage;
import com.wsfk.ssm.path.CallPath;
import com.wsfk.ssm.path.POPS;
import com.wsfk.ssm.service.client.ClientSvc;
import com.wsfk.ssm.service.client.InAppPurchaseSvc;
import com.wsfk.ssm.tools.BaseAction;

/**
 * 
 * @author zhangbao 这里是所有客户端的接口
 */
@Controller("ClientInterfaceAction")
public class ClientInterfaceAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static Logger logger = Logger.getLogger(ClientInterfaceAction.class);

	private HandBookMessage handbookMsg;// 手册下载的封装类
	private Message message;// 修改密码
	private LoginMessage loginMsg;// 登录的信息封装类
	private RegistMessage registMsg;// 注册信息封装类
	private UpdateMessage updateMsg;// 单个更新和发布提示信息类封装
	private List<BookInfo> books;
	private BookInfo book;
	private ClientSvc clientSvc;
	private InAppPurchaseSvc inAppPurchaseSvc;

	private String downloadnum;
	private String version;
	private User user;
	private String userName;
	private String pwd;
	private String newPwd;
	private String appId;

	private String ticket;
	private String password;
	private String bookId;
	private POPS pops;

	public POPS getPops() {
		return pops;
	}

	@Resource
	public void setPops(POPS pops) {
		this.pops = pops;
	}

	private String httpImage;

	public String getHttpImage() {
		return httpImage;
	}

	public void setHttpImage(String httpImage) {
		this.httpImage = httpImage;
	}

	public String getTicket() {
		return ticket;
	}

	public void setTicket(String ticket) {
		this.ticket = ticket;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getBookId() {
		return bookId;
	}

	public void setBookId(String bookId) {
		this.bookId = bookId;
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	private boolean isOk = false;

	private List<Product> produces;

	public List<Product> getProduces() {
		return produces;
	}

	public void setProduces(List<Product> produces) {
		this.produces = produces;
	}

	@Resource
	public void setInAppPurchaseSvc(InAppPurchaseSvc inAppPurchaseSvc) {
		this.inAppPurchaseSvc = inAppPurchaseSvc;
	}

	@Resource
	public void setClientSvc(ClientSvc clientSvc) {
		this.clientSvc = clientSvc;
	}

	// 根据下载码 获取手册下载相关地址的接口
	public String handbook() {
		httpImage = new CallPath().httpPre(pops).build();

		handbookMsg = clientSvc.queryBooksByDownNum(downloadnum);
		
		return "handbook";
	}

	// 注册接口
	public String doRegist() {
		registMsg = clientSvc.addUser(user);
		return "regist";
	}

	// 登录接口
	public String doLogin() {
		loginMsg = clientSvc.queryUserByUserNameAndPwd(userName, pwd);
		return "login";
	}

	// 下载列表接口
	public String bookList() {
		httpImage = new CallPath().httpPre(pops).build();
		
		books = clientSvc.queryBookInfos(userName, pwd);
		
		return "bookList";
	}

	// 单个更新列表查询
	public String book() {
		updateMsg = clientSvc
				.queryBookInfo(userName, pwd, version, downloadnum);
		return "book";
	}

	// 发布审核接口
	/**
	 * 测试
	 * http://localhost:8085/client/interface_publish?userName=testuser@gmail.
	 * com&pwd=202cb962ac59075b964b07152d234b70&downloadnum=6Deykd8m
	 */
	public String publish() {
		httpImage = new CallPath().httpPre(pops).build();
		updateMsg = clientSvc.updatePublishState(userName, pwd, downloadnum, HandBook.PUBLISH);
	
		return "publish";
	}

	// 修改密码
	public String updatePwd() {
		message = clientSvc.updatePwd(userName, pwd, newPwd);
		return "updatepwd";
	}

	// 注册接口
	public String doRegister() {
		registMsg = clientSvc.addUser(user);
		return "register";
	}

	/**
	 * 查看商品列表
	 * 
	 * @return
	 */
	public String showProduces() {
		produces = inAppPurchaseSvc.queryProduces(appId);
		return "showproduces";
	}

	/**
	 * 购买/续费
	 * 
	 * @return
	 */
	public String buyProduces() {

		try {

			User u = inAppPurchaseSvc.getUserByUserName(userName);
			// boolean v = inAppPurchaseSvc.verifyApple(ticket, u.getId());
			logger.debug("user:" + u);
			// TODO NULL
			String billid = inAppPurchaseSvc.addBill(ticket, u.getId(),
					new Date());
			logger.debug("bill id is :" + billid);
			String json = inAppPurchaseSvc.ticketCheck(ticket);
			logger.debug("apple app return json :" + json);
			boolean v = inAppPurchaseSvc.jsonPars(json);
			logger.debug(" is code can used?:" + v);
			if (v) {
				Bill bill = inAppPurchaseSvc.addOder(json, new Date(), billid);
				logger.debug("at last bill is :" + bill);
				int i = inAppPurchaseSvc.buyBook(userName, password, bookId);
				logger.debug("what buy a book use app ?" + i);
				isOk = true;
			} else {
				isOk = false;
			}
		} catch (Exception e) {
			logger.error(e);
			isOk = false;
		}
		return "buyproduces";
	}

	public boolean isOk() {
		return isOk;
	}

	public void setOk(boolean isOk) {
		this.isOk = isOk;
	}

	public HandBookMessage getHandbookMsg() {
		return handbookMsg;
	}

	public void setHandbookMsg(HandBookMessage handbookMsg) {
		this.handbookMsg = handbookMsg;
	}

	public String getDownloadnum() {
		return downloadnum;
	}

	public void setDownloadnum(String downloadnum) {
		this.downloadnum = downloadnum;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public RegistMessage getRegistMsg() {
		return registMsg;
	}

	public void setRegistMsg(RegistMessage registMsg) {
		this.registMsg = registMsg;
	}

	public List<BookInfo> getBooks() {
		return books;
	}

	public void setBooks(List<BookInfo> books) {
		this.books = books;
	}

	public BookInfo getBook() {
		return book;
	}

	public void setBook(BookInfo book) {
		this.book = book;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public UpdateMessage getUpdateMsg() {
		return updateMsg;
	}

	public void setUpdateMsg(UpdateMessage updateMsg) {
		this.updateMsg = updateMsg;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public LoginMessage getLoginMsg() {
		return loginMsg;
	}

	public void setLoginMsg(LoginMessage loginMsg) {
		this.loginMsg = loginMsg;
	}

}
